/**
 * 
 */
package sailing.filters.ui;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import org.jdesktop.application.SingleFrameApplication;

import sailing.filters.ui.laf.FilterPanelUI;
import sailing.ui.AbstractJPanel;

/**
 * @author kevin
 * 
 */
public class FilterTablePanel extends AbstractJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static boolean showFilterPanel = true;

	private JTable table;

	JSplitPane splitPane;

	FilterPanel filterPanel;

	/**
	 * @param application
	 */
	public FilterTablePanel(SingleFrameApplication application) {
		super(application);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.AbstractJPanel#initialPanel()
	 */
	@Override
	protected void initialPanel() {
		JPanel root = new JPanel();
		root.setLayout(new BorderLayout());

		if (showFilterPanel) {
			// add splite component
			this.addSplitPanel(root);
		} else {
			this.addTablePanel(root);
		}
		this.setLayout(new BorderLayout());
		this.add(root, BorderLayout.CENTER);
	}

	private void addSplitPanel(JPanel root) {
		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPane.setContinuousLayout(false);
		splitPane.setOneTouchExpandable(true);
		//
		// splitPane.setDividerLocation(200);
		splitPane.setDividerSize(4);
		// filter panel
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		
		addFilterTablePanel(top);
		splitPane.setTopComponent(top);
		// table panel
		JPanel bottom = new JPanel();
		bottom.setBorder(BorderFactory.createEmptyBorder(3, 0, 0, 0));
		bottom.setLayout(new BorderLayout());
		addTablePanel(bottom);
		splitPane.setBottomComponent(bottom);
		// splitPane.setUI(new PlasticSplitPaneUI());

		root.add(splitPane, BorderLayout.CENTER);
	}

	private void addFilterTablePanel(JPanel root) {
		if (this.filterPanel == null) {
			this.filterPanel = new FilterPanel(this.getApplication());
			
		}
		
		root.add(this.filterPanel, BorderLayout.CENTER);
		root.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		root.setUI(new FilterPanelUI());
	}

	private void addTablePanel(JPanel root) {
		table = new JTable();
		JScrollPane scrollpane = new JScrollPane(table);
		scrollpane.setBackground(this.getBackground());
		root.add(scrollpane, BorderLayout.CENTER);
	}

	public synchronized void relayout(boolean showFilterTable) {
		if (showFilterPanel == showFilterTable) {
			return;
		}
		showFilterPanel = showFilterTable;
		this.removeAll();
		this.initialPanel();
		this.validate();
	}

	public boolean getShowed() {
		return showFilterPanel;
	}

}
